package com.example.homework_4_permission.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                // number和vip接口第一层校验: 登录校验
                .addPathPatterns("/users/number")
                .addPathPatterns("/users/vip")
                // 排除公共静态资源
                .excludePathPatterns(
                        "/index.html",
                        "/css/**",
                        "/js/**",
                        "/pages/tourist.html",
                        "/user/login",
                        "/user/register");
/*        // 访问number.html时会判断是否是number或vip角色
        SaRouter.match("/pages/number.html", r -> StpUtil.checkRoleOr("number","vip"));
        // 访问vip.html时会判断是否是vip角色
        SaRouter.match("/pages/vip.html", r -> StpUtil.checkRole("vip"));*/
    }
}
